package com.superteam.mac.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.superteam.mac.base.web.action.BaseAction;
import com.superteam.mac.mode.MachineRec;
import com.superteam.mac.mode.WorkShop;

public class XiaoLvAction extends BaseAction {
	//获得所有机床 和 车间
	public ActionForward findAllWorkShop(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<WorkShop> workshops = super.getWrokShopService().findWorkShopAndMachines();
		
		request.setAttribute("shops", workshops);
		return mapping.findForward("left");
	}
	
	public ActionForward xiaolvStatic(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<WorkShop> workshops = super.getWrokShopService().findWorkShopAndMachines();
		
		List<MachineRec> machines = new ArrayList<MachineRec>();
		for(WorkShop work:workshops)
		{
			machines.addAll(work.getMachines());
			
		}
		//System.out.println("机床总数。。"+machines.size());
		request.setAttribute("machines", machines);
		return mapping.findForward("xiaolv");
	}
	
	public ActionForward xiaolvStaticImg(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		List<WorkShop> workshops = super.getWrokShopService().findWorkShopAndMachines();
		
		List<MachineRec> machines = new ArrayList<MachineRec>();
		for(WorkShop work:workshops)
		{
			machines.addAll(work.getMachines());
			
		}
		StringBuffer sb = new StringBuffer();
		//palette="2" caption="Country Comparison" shownames="1" showvalues="0" decimals="0" numberPrefix="$" useRoundEdges="1" legendBorderAlpha="0"
		sb.append("<chart palette='2' caption='效率指标' shownames='1' showvalues='0' decimals='0' useRoundEdges='1' legendBorderAlpha='0'>");
		sb.append("<categories>");
		for(MachineRec mac:machines)
		{
			sb.append("<category label='"+mac.getMachineName()+"' /> ");
		}
		sb.append("</categories>");
		
		int i = 10;
		sb.append("<dataset seriesName='开机率' color='ffff00' showValues='0'>");
		for(MachineRec mac:machines)
		{
			sb.append("<set value='"+(i+=5)+"' /> ");
		}
		sb.append("</dataset>");
		
		int k = 15;
		sb.append("<dataset seriesName='利用率' color='0000FF' showValues='0'>");
		for(MachineRec mac:machines)
		{
			sb.append("<set value='"+(k+=5)+"' /> ");
		}
		sb.append("</dataset>");
		
		sb.append(" </chart>");

		request.setAttribute("xml", sb.toString());
		request.setAttribute("machines", machines);
		return mapping.findForward("xiaolv2");
	}
}
